//
//  JMCarKeyViewController.m
//  OpenSesame
//
//  Created by zhangjiaming on 15/12/23.
//  Copyright © 2015年 OpenSesame. All rights reserved.
//

#import "JMCarKeyViewController.h"
#import "JMBrandGroup.h"
#import "JMBrand.h"
#import "JMDisplayKeyViewController.h"
#import "JMCarViewCell.h"
#import "JMSearchViewController.h"

@interface JMCarKeyViewController () <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) NSArray *brandGroup; // 商标
@property (weak, nonatomic) UITableView *tableView;
@end

@implementation JMCarKeyViewController

- (NSArray *)brandGroup {
    if (!_brandGroup) {
        _brandGroup = [JMBrandGroup brandGroupsList];
    }
    return _brandGroup;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = JMColor(242, 242, 242);
    
    // 设置界面
    [self setUpInterface];
}


/**
 *  点击右侧索引跳转对应位置
 *
 *  @param letterBtn 索引值为按钮的tag值减去1000
 */
- (void)letterBtnClick:(UIButton *)letterBtn {
    // 获取所点目录对应的indexPath值
    NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:letterBtn.tag - 1000];
    
    // 让table滚动到对应的indexPath位置
    [self.tableView scrollToRowAtIndexPath:selectIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

/**
 *  跳入汽车搜索控制器
 */
- (void)carCheckbox {
    JMSearchViewController *search = [[JMSearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}

/**
 *  设置界面
 */
- (void)setUpInterface {
    // 设置导航栏内容
    self.title = @"汽车钥匙";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"搜索"] style:UIBarButtonItemStylePlain target:self action:@selector(carCheckbox)];
    
    // 设置tableView
    UITableView *tableview = [[UITableView alloc] init];
    self.tableView = tableview;
    tableview.dataSource = self;
    tableview.delegate = self;
    tableview.frame = CGRectMake(0, 0, JMPhoneWidth - 20, JMPhoneHeight);
    [self.view addSubview:tableview];
    tableview.showsVerticalScrollIndicator = NO;
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 设置右侧的索引栏
    UIScrollView *rightView = [[UIScrollView alloc] init];
    [self.view addSubview:rightView];
    rightView.showsVerticalScrollIndicator = NO;
    rightView.frame = CGRectMake(JMPhoneWidth - 20, 71, 20, JMPhoneHeight); // y值中有7为最顶部的VIew的高度
    rightView.backgroundColor = [UIColor whiteColor];
    CGFloat letterBtnW = 20;
    CGFloat letterBtnH = (JMPhoneHeight - rightView.y)/self.brandGroup.count;
    CGFloat letterBtnX = 0;
    for (int i = 0 ; i < self.brandGroup.count; i++) {
        UIButton *letterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightView addSubview:letterBtn];
        letterBtn.width = letterBtnW;
        letterBtn.height = letterBtnH;
        letterBtn.x = letterBtnX;
        letterBtn.y = letterBtnH * i;
        letterBtn.tag = i + 1000;
        [letterBtn setTitle:[self.brandGroup[i] firstchar] forState:UIControlStateNormal];
        [letterBtn setTitleColor:JMColor(153, 153, 153) forState:UIControlStateNormal];
        letterBtn.titleLabel.font = [UIFont systemFontOfSize:9];
        [letterBtn addTarget:self action:@selector(letterBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

/**
 *  返回上一层
 */
- (void)returnBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.brandGroup.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    JMBrandGroup *brandGroup = self.brandGroup[section];
    return brandGroup.brands.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JMCarViewCell *cell = [JMCarViewCell cellWithTableView:tableView];
    cell.iconView.image = [UIImage imageNamed:@"baoma"];
    JMBrandGroup *brandGroup = self.brandGroup[indexPath.section];
    JMBrand *brand = brandGroup.brands[indexPath.row];
    cell.nameLabel.text = brand.name;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

//点击右侧索引表项时调用
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.brandGroup[section] title];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *customView = [[UIView alloc] init];
    customView.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] init];
    label.opaque = NO;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = JMColor(87, 166, 212);
    label.text = [self.brandGroup[section] firstchar];
    [customView addSubview:label];
    
    if (0 == section) {
        // 最顶部顶部灰色
        UIView *backView = [[UIView alloc] init];
        backView.width = JMPhoneWidth - 20; // 20为右侧索引的宽度
        backView.height = 7;
        backView.x = 0;
        backView.y = 0;
        backView.backgroundColor = JMColor(242, 242, 242);
        label.frame = CGRectMake(0, 17, 40, 20);
        
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 47, backView.width, 1);
        view.backgroundColor = JMColor(242, 242, 242);
        [backView addSubview:view];
        [customView addSubview:backView];
    } else {
        // 每个分组之间的颜色
        UIView *backView = [[UIView alloc] init];
        backView.width = JMPhoneWidth - 20; // 20为右侧索引的宽度
        backView.height = 5;
        backView.x = 0;
        backView.y = 0;
        backView.backgroundColor = JMColor(242, 242, 242);
        label.frame = CGRectMake(0, 15, 40, 20);
        
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 45, backView.width, 1);
        view.backgroundColor = JMColor(242, 242, 242);
        [customView addSubview:view];
        [customView addSubview:backView];
    }
    return customView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (0 == section) {
        return 47;
    }
    return 45;
}

/**
 *  选择cell,跳转到展示钥匙页面
 *
 *  @param tableView tableView
 *  @param indexPath cell所在的索引位置
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 返回时cell没有了背景颜色
    [tableView reloadData];
    JMDisplayKeyViewController *displayKeyVC = [[JMDisplayKeyViewController alloc] init];
    JMBrandGroup *brandGroup = self.brandGroup[indexPath.section];
    JMBrand *brand = brandGroup.brands[indexPath.row];
    displayKeyVC.title = brand.name;
    displayKeyVC.cars = brand.models;
    [self.navigationController pushViewController:displayKeyVC  animated:YES];
}

@end
